﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Utilities
{
    /// <summary>
    /// 此类实现金额的阿拉伯数字到大写汉字的转换
    /// </summary>
    public static class MoneyHelper
    {
        /// <summary>
        /// 转换数字金额主函数（包括小数）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ConvertSum(string str)
        {
            if (!IsPositveDecimal(str))
            {
                return "零";
            }

            if (Double.Parse(str) > 999999999999.99)
            {
                return "数字太大，无法换算，请输入一万亿元以下的金额";
            }

            char[] ch = new char[1];

            //小数点
            ch[0] = '.';

            //定义按小数点分割后的字符串数组 

            //按小数点分割字符串 
            string[] splitstr = str.Split(ch[0]);

            //只有整数部分 
            if (splitstr.Length == 1)
            {
                return ConvertData(str) + "圆整";
            }

            //转换整数部分 
            string rstr = ConvertData(splitstr[0]) + "圆";

            //转换小数部分 
            rstr += ConvertXiaoShu(splitstr[1]);

            //rstr = rstr.Replace("零万", "万");

            return rstr;
        }
        /// <summary>
        /// 需要转换的小数部分数字字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string ConvertXiaoShu(string str)
        {
            int strlen = str.Length;
            string rstr;
            if (strlen == 1)
            {
                rstr = ConvertChinese(str) + "角";
                return rstr;
            }

            string tmpstr = str.Substring(0, 1);
            rstr = ConvertChinese(tmpstr) + "角";
            tmpstr = str.Substring(1, 1);
            rstr += ConvertChinese(tmpstr) + "分";
            rstr = rstr.Replace("零分", "");
            rstr = rstr.Replace("零角", "");

            return rstr;
        }
        /// <summary>
        /// 判断是否是正数字字符串 
        /// </summary>
        /// <param name="str"></param>
        /// <returns>如果是数字，返回true，否则返回false </returns>
        private static bool IsPositveDecimal(string str)
        {
            Decimal d;
            if (Decimal.TryParse(str, out d))
                return d > 0;
            else
                return false;
        }
        /// 转换数字（整数） 
        /// 需要转换的整数数字字符串 
        /// 转换成中文大写后的字符串 
        private static string ConvertData(string str)
        {
            string rstr = "";
            int strlen = str.Length;

            //数字长度小于四位
            if (strlen <= 4)
            {
                rstr = ConvertDigit(str);

            }
            else
            {
                string tmpstr;

                //数字长度大于四位，小于八位 
                if (strlen <= 8)
                {
                    //先截取最后四位数字
                    tmpstr = str.Substring(strlen - 4, 4);

                    //转换最后四位数字
                    rstr = ConvertDigit(tmpstr);

                    //截取其余数字 
                    tmpstr = str.Substring(0, strlen - 4);

                    //将两次转换的数字加上万后相连接 
                    rstr = String.Concat(ConvertDigit(tmpstr) + "万", rstr);
                    rstr = rstr.Replace("零万", "万");
                    rstr = rstr.Replace("零零", "零");
                }
                else
                {
                    //数字长度大于八位，小于十二位 
                    if (strlen <= 12)
                    {
                        //先截取最后四位数字 
                        tmpstr = str.Substring(strlen - 4, 4);

                        //转换最后四位数字 
                        rstr = ConvertDigit(tmpstr);

                        //再截取四位数字 
                        tmpstr = str.Substring(strlen - 8, 4);
                        rstr = String.Concat(ConvertDigit(tmpstr) + "万", rstr);
                        tmpstr = str.Substring(0, strlen - 8);
                        rstr = String.Concat(ConvertDigit(tmpstr) + "亿", rstr);
                        rstr = rstr.Replace("零亿", "亿");
                        rstr = rstr.Replace("零万", "万");
                        rstr = rstr.Replace("零零", "零");
                        rstr = rstr.Replace("零零", "零");
                    }
                }
            }

            strlen = rstr.Length;
            if (strlen >= 2)
            {
                switch (rstr.Substring(strlen - 2, 2))
                {
                    case "佰零": rstr = rstr.Substring(0, strlen - 2) + "佰"; break;
                    case "仟零": rstr = rstr.Substring(0, strlen - 2) + "仟"; break;
                    case "万零": rstr = rstr.Substring(0, strlen - 2) + "万"; break;
                    case "亿零": rstr = rstr.Substring(0, strlen - 2) + "亿"; break;
                }
            }
            return rstr;
        }
        /// <summary>
        /// 转换的字符串（四位以内）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string ConvertDigit(string str)
        {
            int strlen = str.Length;
            string rstr = "";
            switch (strlen)
            {
                case 1: rstr = ConvertChinese(str); break;
                case 2: rstr = Convert2Digit(str); break;
                case 3: rstr = Convert3Digit(str); break;
                case 4: rstr = Convert4Digit(str); break;
            }
            rstr = rstr.Replace("拾零", "拾");
            return rstr;
        }
        /// <summary>
        /// 转换四位数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string Convert4Digit(string str)
        {
            string str1 = str.Substring(0, 1);
            string str2 = str.Substring(1, 1);
            string str3 = str.Substring(2, 1);
            string str4 = str.Substring(3, 1);
            string rstring = "";
            rstring += ConvertChinese(str1) + "仟";
            rstring += ConvertChinese(str2) + "佰";
            rstring += ConvertChinese(str3) + "拾";
            rstring += ConvertChinese(str4);
            rstring = rstring.Replace("零仟", "零");
            rstring = rstring.Replace("零佰", "零");
            rstring = rstring.Replace("零拾", "零");
            rstring = rstring.Replace("零零", "零");
            rstring = rstring.Replace("零零", "零");
            rstring = rstring.Replace("零零", "零");
            return rstring;
        }
        /// <summary>
        /// 转换三位数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string Convert3Digit(string str)
        {
            string str1 = str.Substring(0, 1);
            string str2 = str.Substring(1, 1);
            string str3 = str.Substring(2, 1);
            string rstring = "";
            rstring += ConvertChinese(str1) + "佰";
            rstring += ConvertChinese(str2) + "拾";
            rstring += ConvertChinese(str3);
            rstring = rstring.Replace("零佰", "零");
            rstring = rstring.Replace("零拾", "零");
            rstring = rstring.Replace("零零", "零");
            rstring = rstring.Replace("零零", "零");
            return rstring;
        }
        /// <summary>
        /// 转换二位数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string Convert2Digit(string str)
        {
            string str1 = str.Substring(0, 1);
            string str2 = str.Substring(1, 1);
            string rstring = "";
            rstring += ConvertChinese(str1) + "拾";
            rstring += ConvertChinese(str2);
            rstring = rstring.Replace("零拾", "零");
            rstring = rstring.Replace("零零", "零");
            return rstring;
        }
        /// <summary>
        /// 将一位数字转换成中文大写数字 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string ConvertChinese(string str)
        {
            //"零壹贰叁肆伍陆柒捌玖拾佰仟万亿圆整角分" 
            string cstr = "";
            switch (str)
            {
                case "0":
                    cstr = "零";
                    break;
                case "1":
                    cstr = "壹";
                    break;
                case "2":
                    cstr = "贰";
                    break;
                case "3":
                    cstr = "叁";
                    break;
                case "4":
                    cstr = "肆";
                    break;
                case "5":
                    cstr = "伍";
                    break;
                case "6":
                    cstr = "陆";
                    break;
                case "7":
                    cstr = "柒";
                    break;
                case "8":
                    cstr = "捌";
                    break;
                case "9":
                    cstr = "玖";
                    break;
            }

            return (cstr);
        }
    }
}
